package com.qf.kdxf.controller;

import com.qf.kdxf.entity.facerecognition.ResponseData;
import com.qf.kdxf.service.FaceRecognitionService;
import com.qf.kdxf.util.common.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.HashMap;

/**
 * @Author 明廷盛
 * @Date 2025/6/16 13:30
 * @Description
 */
@RestController
@RequestMapping("/face")
public class FaceRecognitionController {
    @Autowired
    FaceRecognitionService faceService;

    /**
     * 01-人脸对比-图片上传
     *
     * @param imageFile
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile imageFile) {
        String path = "D:\\用户\\文档\\CodeBeach\\Java\\kdxf(4)\\kdxf\\src\\main\\resources\\static\\image\\";

        String filePath = UploadFile.upload(imageFile, path);
        return filePath;
    }

    /**
     * 01-人脸对比-人脸相似度比较
     *
     * @param imagepath1
     * @param imagepath2
     * @return
     */
    @RequestMapping("/compare")
    @ResponseBody
    public String compare(String imagepath1, String imagepath2) {
        ResponseData respData = null;
        System.err.println("for test");
        System.out.println(imagepath1);
        System.out.println(imagepath2);
        try {
            respData = faceService.faceContrast(imagepath1, imagepath2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String text = null;
        if (respData != null && respData.getPayLoad().getFaceCompareResult() !=
                null) {
            String textBase64 =
                    respData.getPayLoad().getFaceCompareResult().getText();
            text = new String(Base64.getDecoder().decode(textBase64));
        }
        return text;
    }

    /**
     * 02-人脸检测
     * @param faceImage
     * @param request
     * @return
     */
    @RequestMapping("/getFeatures")
    @ResponseBody
    public HashMap<String, String> getFeatures(@RequestParam("faceImage") MultipartFile faceImage, HttpServletRequest request){
        String filename = faceImage.getOriginalFilename();
        String path = request.getServletContext().getRealPath("/");
        String filePath = UploadFile.upload(faceImage,path);
        HashMap<String, String> features = faceService.getFeatures(filePath, filename);
        System.out.println(features);
        return features;
    }

    /**
     * 03-人脸特征分析（年龄、颜值、性别、表情）
     * @param detectFile
     * @param request
     * @return
     */
    @RequestMapping("/getFaceDetect")
    @ResponseBody
    public String getFaceDetect(@RequestParam("detectFile") MultipartFile detectFile, HttpServletRequest request){
        String filename = String.valueOf(System.currentTimeMillis());
        String path = request.getServletContext().getRealPath("/")+filename+".mp3";
        String imagePath = UploadFile.upload(detectFile,path);


        ////////////////////////////////////////////////
        ////////////////////////////////////////////////

        ResponseData respData = faceService.faceContrast(imagePath);
        String text = null;
        if (respData!=null && respData.getPayLoad().getFace_detect_result() != null) {
            String textBase64 = respData.getPayLoad().getFace_detect_result().getText();
            text = new String(Base64.getDecoder().decode(textBase64));
            System.out.println("人脸检测及属性分析结果(text)base64解码后：");
            System.out.println(text);
        }
        return text;
    }

}
